#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 将splitter填充满整个工作区
    setCentralWidget(ui->splitter);

    // 将Tool Button与Action绑定
    setActionssForButton();

    // 为Tool Button 创建下拉菜单
    createSelectionPopMenu();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setActionssForButton()
{
    ui->tBtnListIni->setDefaultAction(ui->actListIni);
    ui->tBtnListIni->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    ui->tBtnListClear->setDefaultAction(ui->actListClear);
    ui->tBtnListClear->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    ui->tBtnListInsert->setDefaultAction(ui->actListInsert);
    ui->tBtnListInsert->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    ui->tBtnListAppend->setDefaultAction(ui->actListAppend);
    ui->tBtnListAppend->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    ui->tBtnListDelete->setDefaultAction(ui->actListDelete);
    ui->tBtnListDelete->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    ui->tBtnSelALL->setDefaultAction(ui->actSelALL);
    ui->tBtnSelNone->setDefaultAction(ui->actSelNone);
    ui->tBtnSelInvs->setDefaultAction(ui->actSelInvs);
}

void MainWindow::createSelectionPopMenu()
{
    QMenu* menuSleection = new QMenu(this);
    menuSleection->addAction(ui->actSelALL);
    menuSleection->addAction(ui->actSelNone);
    menuSleection->addAction(ui->actSelInvs);

    //ui->tBtnSelectItem->setPopupMode(QToolButton::InstantPopup);      // MenuButtonPopup 直接单击会执行按钮关联的Action,不会弹出下拉菜单
    ui->tBtnSelectItem->setPopupMode(QToolButton::InstantPopup);          // InstantPopup 当按钮按下时没有延迟，菜单立即显示。在这个模式下，按钮自身的动作不会被触发（clicked等）
    ui->tBtnSelectItem->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    ui->tBtnSelectItem->setDefaultAction(ui->actSelPopMenu);
    ui->tBtnSelectItem->setMenu(menuSleection);

    // 在工具栏上添加一个下拉式菜单
    /*
    QToolButton *aBtn = new QToolButton(this);
    aBtn->setPopupMode(QToolButton::InstantPopup);
    aBtn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
    aBtn->setDefaultAction(ui->actSelPopMenu);
    aBtn->setMenu(menuSleection);
    */

    // 工具栏添加一个分隔符
    ui->toolBar->addSeparator();
    ui->toolBar->addAction(ui->actQuit);
}


void MainWindow::on_actListIni_triggered()
{
    QListWidgetItem *aItem;
    QIcon aIcon;
    aIcon.addFile(":/images/icons/check2.ico");
    bool chk = ui->chkBoxListEditable->isChecked();

    ui->listWidget->clear();
    for(int i=0; i<10; i++)
    {
        QString str = QString::asprintf("Item %d",i);
        aItem = new QListWidgetItem();
        aItem->setText(str);
        aItem->setIcon(aIcon);
        aItem->setCheckState(Qt::Checked);
        if(chk)
            aItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable
                            |Qt::ItemIsUserCheckable|Qt::ItemIsEnabled);
        else
            aItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsUserCheckable|Qt::ItemIsEnabled);
        ui->listWidget->addItem(aItem);
    }
}

void MainWindow::on_actListInsert_triggered()
{
    QIcon aIcon;
    aIcon.addFile(":/image/icons/check2.icon");
    bool chk = ui->chkBoxListEditable->isChecked();

    QListWidgetItem* aItem = new QListWidgetItem("New Inserted Item");
    aItem->setIcon(aIcon);
    aItem->setCheckState(Qt::Checked);
    if(chk)
        aItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable
                        |Qt::ItemIsUserCheckable|Qt::ItemIsEnabled);
    else
        aItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsUserCheckable|Qt::ItemIsEnabled);
    ui->listWidget->insertItem(ui->listWidget->currentRow(),aItem);
}

void MainWindow::on_actListClear_triggered()
{
    ui->listWidget->clear();
}

void MainWindow::on_actListDelete_triggered()
{
    int row = ui->listWidget->currentRow();
    QListWidgetItem* aItem = ui->listWidget->takeItem(row);
    delete aItem;
}

void MainWindow::on_actListAppend_triggered()
{
    QIcon aIcon;
    aIcon.addFile(":/image/icons/check2.icon");
    bool chk = ui->chkBoxListEditable->isChecked();

    QListWidgetItem* aItem = new QListWidgetItem("Append Item");
    aItem->setIcon(aIcon);
    aItem->setCheckState(Qt::Checked);
    if(chk)
        aItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable
                        |Qt::ItemIsUserCheckable|Qt::ItemIsEnabled);
    else
        aItem->setFlags(Qt::ItemIsSelectable|Qt::ItemIsUserCheckable|Qt::ItemIsEnabled);
    ui->listWidget->addItem(aItem);
}

void MainWindow::on_actSelALL_triggered()
{
    int cnt = ui->listWidget->count();
    for(int i=0;i<cnt;i++)
    {
        QListWidgetItem *aItem = ui->listWidget->item(i);
        aItem->setCheckState(Qt::Checked);
    }
}

void MainWindow::on_actSelNone_triggered()
{
    int cnt = ui->listWidget->count();
    for(int i=0;i<cnt;i++)
    {
        QListWidgetItem *aItem = ui->listWidget->item(i);
        aItem->setCheckState(Qt::Unchecked);
    }
}

void MainWindow::on_actSelInvs_triggered()
{
    int cnt = ui->listWidget->count();
    for(int i=0;i<cnt;i++)
    {
        QListWidgetItem *aItem = ui->listWidget->item(i);
        if(aItem->checkState() != Qt::Checked)
            aItem->setCheckState(Qt::Checked);
        else
            aItem->setCheckState(Qt::Unchecked);
    }
}

void MainWindow::on_listWidget_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
    QString str;
    if(current !=NULL)
    {
        if(previous == NULL)
            str = "当前项：" + current->text();
        else
            str = "前一项：" + previous->text() + "; 当前项：" + current->text();
        ui->editCutItemText->setText(str);
    }
}

// 响应该函数，需要将listWidget的ContextMenuPolicy属性设置为CustomContextMenu
void MainWindow::on_listWidget_customContextMenuRequested(const QPoint &pos)
{
    Q_UNUSED(pos)
    QMenu *menuList = new QMenu();

    menuList->addAction(ui->actListIni);
    menuList->addAction(ui->actListClear);
    menuList->addAction(ui->actListInsert);
    menuList->addAction(ui->actListAppend);
    menuList->addAction(ui->actListDelete);
    menuList->addSeparator();
    menuList->addAction(ui->actSelALL);
    menuList->addAction(ui->actSelNone);
    menuList->addAction(ui->actSelInvs);

    menuList->exec(QCursor::pos());
    delete menuList;
}
